package com.bxd.baodan.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.ImageView;


/**
 * Created by Administrator on 2017/10/31.
 */

@SuppressLint("AppCompatCustomView")
public class ScaleHeightImageView extends ImageView {
    public ScaleHeightImageView(Context context) {
        super(context);
    }


    public ScaleHeightImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ScaleHeightImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Drawable drawable= getDrawable();

        if (drawable != null) {
            float scale = (float)drawable.getIntrinsicHeight()/ drawable.getIntrinsicWidth();
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height =(int)(width * scale);
            heightMeasureSpec =   MeasureSpec.makeMeasureSpec(height,MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
